/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * All Rights Researved.
 *
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 * Description
 *
 * User: jinchen
 * Date: Jun 2, 2003
 * Time: 10:19:02 AM
 */
package edu.psu.geovista.app.plot.axis;

import edu.psu.geovista.app.pcp.toolkits.Validator;
import edu.psu.geovista.app.pcp.toolkits.java2d.Java2DToolkit;

import java.awt.*;

public class AxisYHeader {
    //protected String title;
    //protected Point location; //left-down corner
    protected Dimension size;//header dimension
    protected Font font;
    protected  String style;
    protected Color color;
    protected YAxis axis;
    private Rectangle outLine=new Rectangle(); // 
    private int paintmode=YAxis.PAINT_MODE_FULL;

    public AxisYHeader() {
        //this.title ="";
        //this.location =new Point(0,0);
        this.size=new Dimension(60,14);
        this.style ="Monospaced";
        this.font =new Font(style,Font.BOLD,size.height ); //font size depend on size


    }
    public AxisYHeader(YAxis axis) {
        this();
        this.axis=axis;
        //this.position.setLocation(position);
        //this.resetPosition();

    }

    /********************************************************************
     *                GUI
     ********************************************************************/
    public void repaint() {
    }
    public void paintComponent(Graphics g){
        String orgtitle=this.getTitle();

        if(Validator.isValidString(orgtitle )){
            String title=orgtitle;
            if(this.getPaintmode() ==YAxis.PAINT_MODE_SIMPLE){ //in simple mode, only paint the 1st letter
                title=orgtitle.substring(0,2);
                //System.out.println("orgtitle:"+orgtitle+" t "+title);
            }



            Graphics2D g2d=( Graphics2D)g;
            Font old=g2d.getFont() ;
            g2d.setFont(this.font );
            // String width

            FontMetrics fm=g.getFontMetrics() ;//Use default font, thus no matter what font of title, keep use original font for description
            //only for set outline
            int height = fm.getHeight();
            int width=fm.stringWidth(title );
            //outLine.setSize(width,height);

            int x=this.getAxisLocation().x-Math.round(width/2);
            int y=this.getAxisLocation().y-YAxis.HEADER_PAD ;
           //outLine.setLocation(x,y);
            setOutLine(x,y-height,width,height);    //(outLine.x, outLine.y) should be the upper-left coner of the title



            //g2d.drawLine(x,y,x,position.y-size.height);

            Color oldc = g2d.getColor();
            g2d.setColor(this.getColor() );
            g2d.drawString(title.toUpperCase() ,x,y);
            g2d.setFont(old);
            g2d.setColor(oldc);
            paintDescription(g2d);

           /* Composite c=g2d.getComposite() ;
            DataPainter.drawTooltip(g2d,"test",x+20,y+20,new Color(0.2f,0.2f,0.2f,0.5f),width);

            g2d.setComposite(c);*/

        }
    }

    private void paintDescription(Graphics2D g2d){
        if(this.axis.isShowDescription() ){

           String desc = this.axis.getDescription().trim() ;
           int w=Java2DToolkit.getStringPaintingSize(g2d,desc,null).width ;
           int x=getAxisLocation().x -w/2;
           int y=this.outLine.y+3  ;
           Color bgc=new Color(200,200,200,150);
           Color fc=Color.green;
           Java2DToolkit.paintTooltip(g2d,desc,x,y,null,fc,bgc);
        }
    }

    /********************************************************************
     *                properties
     ********************************************************************/
    public Font getFont() {
        return font;
    }

    public void setFont(Font font) {
        this.font = font;
        this.size.height =font.getSize() ;//+2;
        this.repaint() ;

    }
    public Dimension getSize() {
        return size;
    }

    public void setSize(Dimension size) {
        this.size = size;
        this.font=new Font(this.style,Font.BOLD,size.height ); //font size depend on size
        this.repaint() ;
    }

    public boolean contain(int x, int y) {
        Rectangle outLine = this.getOutLine();
        //System.out.println("Outline:"+outLine.toString() );
        return outLine.contains(x,y);
    }
    /*******************************************************************************************************
     *                outline
     *******************************************************************************************************/
     public Rectangle getOutLine() {
        if (this.outLine ==null)  {
            this.outLine =new Rectangle();
        }

        //this.setOutLine(outLine);
        return outLine;
    }
    /**
     *
     */
    private void setOutLine(int x, int y, int w, int h){
           outLine.setLocation(x,y);
           outLine.setSize(w,h);

    }

     /**
     *  return  location of drawing O point which all drawing is relative to.
     */
    /*private Point getDrawingOLocation(){

        int x=this.getAxisLocation().x-Math.round(stringSize/2);
        int y=this.getAxisLocation().y-YAxis.HEADER_PAD ;
        return new Point(this.getAxisLocation().x,this.getAxisLocation().y+axis.getActiveSize() );
    }*/
    public Point getAxisLocation() {
         return axis.getLocation() ;
        //return location;
    }
    /*
    public void setLocation(Point position) {
        this.location = position;
    }  */

    public String getTitle() {

         return axis.getName() ;
       // return title;
    }

    public int getPaintmode() {
        return paintmode;
    }

    public void setPaintmode(int paintmode) {
        this.paintmode = paintmode;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }


}
